import java.util.ArrayList;
import java.util.List;
import java.util.Random;


public class Main {
	
	private static final int NUM_TEAMS = 3;
	private static final int NUM_EMPLOYEES_PER_DAY = 3;
	private static final int NUM_QUESTIONS_PER_DAY = 20;
	
	public static final int MINUTE_TIME_IN_MILLIS = 10;
	public static final Time START_OF_DAY = new Time(7, 30);
	public static final Time END_OF_DAY = new Time(17, 0);
	
	public static void main(String[] args) throws InterruptedException
	{
		List<Thread> allThreads = new ArrayList<Thread>();
		
		ArrayList<Employee> allMembers = new ArrayList<Employee>();

		Schedule schedule = new Schedule();

		allThreads.add(schedule.getConferenceRoom());

		Manager manager = new Manager(schedule);
		allThreads.add(manager);
		manager.start();
		allMembers.add(manager);

		List<List<Developer>> teams = new ArrayList<List<Developer>>();
		for (int i = 0; i < NUM_TEAMS; i++)
		{
			List<Developer> team = new ArrayList<Developer>();
			for (int j = 0; j < NUM_EMPLOYEES_PER_DAY; j++)
			{
				Developer developer = new Developer(schedule, i + 1);
				developer.setName("Developer " + (i + 1) + (j + 2));
				developer.start();
				team.add(developer);
				allThreads.add(developer);
				allMembers.add(developer);

			}
			teams.add(team);
		}

		ArrayList<TeamLead> leads = new ArrayList<TeamLead>();
		for (int i = 0; i < NUM_TEAMS; i++)
		{
			TeamLead lead = new TeamLead(schedule, i + 1, teams.get(i));
			lead.setName("Developer " + (i + 1) + "1");
			lead.start();
			leads.add(lead);
			allThreads.add(lead);
			allMembers.add(lead);
		}

		ArrayList<Employee> teamLeadStandup = new ArrayList<Employee>();
		teamLeadStandup.addAll(leads);
		teamLeadStandup.add(manager);
		
		// Schedule the morning stand-up meeting
		schedule
			.scheduleEvent(new TeamLeadStandup(START_OF_DAY, teamLeadStandup,
				new TimeDuration(0, 15), "Manager Standup Meeting"));
		
		
		
		schedule.scheduleEvent(new Meeting(new Time(16, 0), allMembers, new TimeDuration(0, 15), "Afternoon Standup Meeting"));
		
		
		// Generate the Questions for the day
		Random questionRand = new Random();
		int numberOfDevs = (NUM_EMPLOYEES_PER_DAY + 1) * NUM_TEAMS; // not 0 based
		Employee randomEmployee;
		Meeting randomQuestion;
		ArrayList<Employee> questionMembers;
		for(int i = 0; i < NUM_QUESTIONS_PER_DAY; i++){
			// Choose a random time, between the start and end of the day
			
			Time questionTime = Time.randomBetween(new Time(9,0), new Time(16,0));//START_OF_DAY.add(new TimeDuration(0, questionMinuteOfDay));
			
			
			// Choose random developer or team lead to get this question
			randomEmployee = allMembers.get(questionRand.nextInt(numberOfDevs) + 1); // is 0 based, 0 being the manager
			
			questionMembers = new ArrayList<Employee>();
			questionMembers.add(randomEmployee);
			
			// Create new question
			if(randomEmployee instanceof TeamLead){ // asker is a team lead
				questionMembers.add(manager);
//				randomQuestion = new UnansweredQuestion(
//						questionTime,
//						questionMembers);
				randomQuestion = new Question(questionTime,questionMembers,schedule,randomEmployee, (TeamLead)randomEmployee, manager);
				schedule.scheduleEvent(new QuestionThought(questionTime, (Developer)randomEmployee, (TeamLead)randomEmployee, manager));
			}else{ // asker is not the lead
				int teamIndex = ((Developer)randomEmployee).getTeam();
				TeamLead randLeader = leads.get(teamIndex - 1);
				questionMembers.add(randLeader);
				randomQuestion = new Question(questionTime, questionMembers, schedule, randomEmployee, randLeader, manager);
				schedule.scheduleEvent(new QuestionThought(questionTime, (Developer)randomEmployee, randLeader, manager));
			}
			System.out.println("Question " + i + " @" + questionTime + " asked by " + randomEmployee.getName());
			
			// Schedule new question
			//schedule.scheduleEvent(randomQuestion);
			//schedule.scheduleEvent(new QuestionThought(questionTime, questionMembers, manager));
		}
		
		

		schedule.start();

		for (Thread thread : allThreads)
		{
			try
			{
				thread.join();
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
		}
		
		
	}

}
